package com.by.yz.activity;


import android.content.Context;
import android.os.Bundle;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.toolbox.NetworkImageView;
import com.bumptech.glide.Glide;
import com.by.yz.App;
import com.by.yz.R;
import com.by.yz.activity.base.BaseActivity;
import com.by.yz.chatuidemo.Constant;
import com.by.yz.chatuidemo.ui.ChatActivity;
import com.by.yz.custom.CircleNetworkImageView;
import com.by.yz.custom.TechnologyTeamDividerItemDecoration;
import com.by.yz.entity.Account;
import com.by.yz.entity.AskBean;
import com.by.yz.entity.UserTest;
import com.by.yz.loginUtil.AccountRESTFul;
import com.by.yz.loginUtil.QustionRESFul;
import com.by.yz.loginUtil.UserRESTFul;
import com.by.yz.util.ToJSonObject;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class QuikListActivity extends BaseActivity {
    RecyclerView quikList;
    SimpleStringDoctorListViewAdapter simpleStringDoctorListViewAdapter;
    List<AskBean> datas=new ArrayList<AskBean>();
    private  boolean flag;
    private String name;
    UserTest userTest,userTest1;
    String img=null,img1=null;
    static String nick=null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.famous_list);
        Log.e("onCreate","onCreate");

        initViews();
        initData();
    }

    private void initViews() {
        setToolbarTitle("问题列表");
        Log.e("initViews","initViews");
        quikList=(RecyclerView)findViewById(R.id.famousdoctor_listview);
        quikList.setLayoutManager(new LinearLayoutManager(quikList.getContext()));
        simpleStringDoctorListViewAdapter=new SimpleStringDoctorListViewAdapter(this,datas);
        quikList.setAdapter(simpleStringDoctorListViewAdapter);
        quikList.addItemDecoration(new TechnologyTeamDividerItemDecoration(this,TechnologyTeamDividerItemDecoration.VERTICAL_LIST));
    }

    private void initData() {
        Log.e("initData","initData");
        Intent intent = getIntent();
        flag = intent.getBooleanExtra("isDoctor",false);
        name = intent.getStringExtra("username");
        Log.e("isDoctor+username=",flag+name);
        UserRESTFul userRESTFul=new UserRESTFul();
        userRESTFul.getUser(name, new UserRESTFul.UserPOSTCallBack() {
            @Override
            public void ok(JSONObject response) throws JSONException {
                userTest=ToJSonObject.jsonToBean(response.toString(),UserTest.class);
                img = userTest.getHeadimg();
                nick = userTest.getnickName();
                Log.e("userTest","userTest");
            }
            @Override
            public void error(String response) {
                Log.e("PCF-user-error","获取用户表失败"+response);
            }
        });
        Log.e("userRESTFul","userRESTFul");
        QustionRESFul qustionRESFul=new QustionRESFul();
        qustionRESFul.getQuestion( new QustionRESFul.MyGetCallBack() {
            @Override
            public void ok(JSONArray response) throws JSONException {
                AskBean askBean ;
                    for (int i = 0; i < response.length(); i++) {
                        Log.e("QustionRESFul",i+"");
                        JSONObject jsonObject = response.getJSONObject(i);
                        askBean = ToJSonObject.jsonToBean(jsonObject.toString(), AskBean.class);
                        if(flag) { //true 为医生
                            if ( askBean.getAnswer().equals(name)) {// 查找该医生回答过的问题
                                img1 = askBean.getQuestion_image();
                                try {
                                    askBean.setQuestion_image(img1.toString());
                                }
                                catch (Exception e){
                                    askBean.setQuestion_image("");
                                }
                                datas.add(askBean);
                            }
                        }else{ //false 为患者
                            askBean.setQuestion_image(img);
                            if ( askBean.getCreater().equals(name)) {// 查找自己的问题
                                datas.add(askBean);
                            }
                        }
                    }
                simpleStringDoctorListViewAdapter.notifyDataSetChanged();
            }
            @Override
            public void error(String error) {
                                Log.e("hj","更新失败"+error);
                            }
        });
        Log.e("QustionRESFul","QustionRESFul");
    }

    public static class SimpleStringDoctorListViewAdapter
            extends RecyclerView.Adapter<SimpleStringDoctorListViewAdapter.ViewHolder> {

            private final TypedValue mTypedValue = new TypedValue();
            private int mBackground;
            private List<AskBean> mValues;

            public static class ViewHolder extends RecyclerView.ViewHolder {
                public AskBean tt;

                public final View mView;
                public final CircleNetworkImageView mImageView;
                public final TextView askName;
                public final TextView askTime;
                public final TextView question;
                public final TextView nickname;

                public ViewHolder(View view) {
                    super(view);
                    mView = view;
                    mImageView = (CircleNetworkImageView) view.findViewById(R.id.answer_image);
                    askName = (TextView) view.findViewById(R.id.askName);
                    askTime = (TextView) view.findViewById(R.id.ask_time);
                    question = (TextView) view.findViewById(R.id.question);
                    nickname = (TextView) view.findViewById(R.id.nickname);
                }
            }

            public AskBean getValueAt(int position) {
                return mValues.get(position);
            }

            public SimpleStringDoctorListViewAdapter(Context context, List<AskBean> items) {
                context.getTheme().resolveAttribute(R.attr.selectableItemBackground, mTypedValue, true);
                mBackground = mTypedValue.resourceId;
                mValues = items;
            }

            @Override
            public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
                View view = LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.quick_answer_item, parent, false);
                // view.setBackgroundResource(mBackground);
                return new ViewHolder(view);
            }

            @Override
            public void onBindViewHolder(final ViewHolder holder, int position) {
                holder.tt = mValues.get(position);
                holder.askName.setText(mValues.get(position).getCreater());
                holder.mImageView.setDefaultImageResId(R.mipmap.headimg);
                holder.mImageView.setErrorImageResId(R.mipmap.headimg_error);
                final String creater=mValues.get(position).getCreater();
                new Thread(){
                    @Override
                    public void run(){
                        UserRESTFul userRESTFul=new UserRESTFul();
                        userRESTFul.getUser(creater, new UserRESTFul.UserPOSTCallBack() {
                            @Override
                            public void ok(JSONObject response) throws JSONException {
                                UserTest userTest;
                                JSONObject jsonObject=response;
                                userTest=ToJSonObject.jsonToBean(jsonObject.toString(),UserTest.class);
                                holder.askName.setText(userTest.getnickName());
                                String image=userTest.getHeadimg();
                                try {
                                    holder.mImageView.setImageUrl(image, App.imageLoader);
                                    Log.e("image","image");
                                }catch(Exception e) {
                                    Log.e("image","null");
                                    holder.mImageView.setImageUrl("http://img2.hao661.com/uploads/allimg/c150515/1431E91Z5M10-2194c1.jpg",App.imageLoader);
                                }
                            }
                            @Override
                            public void error(String response) {
                                Log.e("PCF-user-error","获取用户表失败"+response);
                            }
                        });
                    }
                }.start();
                String s= mValues.get(position).getTitle().replaceAll("T", " ");
                String s1 = s.substring(0,16);
                holder.askTime.setText(s1);
                holder.question.setText(mValues.get(position).getContent());
                holder.mView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Context context = v.getContext();
                        Intent intent = new Intent(context, QuestionDetail.class);
                        Bundle bundle = new Bundle();
                        bundle.putSerializable("question",holder.tt);
                        intent.putExtras(bundle);
                        context.startActivity(intent);
                    }
                });
                Glide.with(holder.mImageView.getContext())
                        .load(mValues.get(position).getQuestion_image())
                        .fitCenter()
                        .into(holder.mImageView);
            }
            @Override
            public int getItemCount() {
                return mValues.size();
            }
        }

}



